package cn.demomaster.quickui_sample.ui.fragment.component

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import cn.demomaster.quicklogger.QdLog
import cn.demomaster.quickrouter.fragment.QuickFragment
import cn.demomaster.quickui.annotation.ActivityPager
import cn.demomaster.quickui.annotation.ResType
import cn.demomaster.quickui_sample.R
import cn.demomaster.quickui_sample.net.Api
import com.demomaster.quickcompat.widget.button.QuickButton
import com.squirrelhuan.quickhttp.http.QuickHttp
import kotlinx.coroutines.launch
import org.json.JSONObject


@ActivityPager(
    name = "图片",
    preViewClass = QuickButton::class,
    resType = ResType.Resource
)
class ImageFragment : QuickFragment() {
    override fun onGenerateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_layout_image, null)
    }
    private val api: Api
        get() {
            val baseUrl = "https://api.demomaster.cn"
            //return QuickHttp(baseUrl).buildRetrofit().create(Api::class.java)

//            val quickHttp:QuickHttp =
//                QuickHttp.Builder().setBaseUrl("https://www.baidu.com/") //.addInterceptor(new QuickHttpLoggingInterceptor())
//                    .build()
            return QuickHttp(baseUrl).buildRetrofit().create(Api::class.java)
        }
    override fun initView(rootView: View?) {
        lifecycleScope.launch {
            try {
                val response = api.authentication(
                    apiKey = "apiKey",
                    clientId = "clientId"
                )
                var token = JSONObject(response.toString())["token"].toString()
                QdLog.i("token: $token")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }


}